# -*- mode: python ; coding: utf-8 -*-

block_cipher = None

# 配置
import os
import sys
import site
from pathlib import Path

# 自动检测Python环境和PyQt5路径
def find_pyqt5_path():
    """自动查找PyQt5路径"""
    # 尝试各种可能的位置
    possible_paths = [
        # 标准Python安装
        Path(sys.prefix) / "Lib" / "site-packages" / "PyQt5",
        # Anaconda安装
        Path(sys.prefix) / "Lib" / "site-packages" / "PyQt5",
        # 用户安装
        *[Path(p) / "PyQt5" for p in site.getsitepackages()],
        # 当前环境
        Path(os.getcwd()) / "env" / "Lib" / "site-packages" / "PyQt5",
    ]
    
    # 检查哪个路径存在
    for path in possible_paths:
        if path.exists():
            return str(path)
    
    # 如果都找不到，使用默认路径
    return os.path.join(sys.prefix, "Lib", "site-packages", "PyQt5")

# 查找PyQt5路径
PYQT5_PATH = find_pyqt5_path()
print(f"Using PyQt5 path: {PYQT5_PATH}")

def get_qt_plugin_path():
    """获取Qt插件路径"""
    # 尝试各种可能的位置
    possible_paths = [
        os.path.join(PYQT5_PATH, "Qt5", "plugins"),
        os.path.join(PYQT5_PATH, "Qt", "plugins"),
        os.path.join(PYQT5_PATH, "plugins"),
        os.path.join(os.path.dirname(PYQT5_PATH), "Qt5", "plugins"),
    ]
    
    # 检查哪个路径存在
    for path in possible_paths:
        if os.path.exists(path):
            return path
    
    # 如果都找不到，返回默认路径
    return os.path.join(PYQT5_PATH, "Qt5", "plugins")

def get_qt_translations_path():
    """获取Qt翻译文件路径"""
    # 尝试各种可能的位置
    possible_paths = [
        os.path.join(PYQT5_PATH, "Qt5", "translations"),
        os.path.join(PYQT5_PATH, "Qt", "translations"),
        os.path.join(PYQT5_PATH, "translations"),
        os.path.join(os.path.dirname(PYQT5_PATH), "Qt5", "translations"),
    ]
    
    # 检查哪个路径存在
    for path in possible_paths:
        if os.path.exists(path):
            return path
    
    # 如果都找不到，返回默认路径
    return os.path.join(PYQT5_PATH, "Qt5", "translations")

# 构建文件列表
binaries = []
datas = [
    # 项目资源文件
    ('resources/*', 'resources'),
]

# 添加Qt插件和DLL（仅当它们存在时）
qt_plugin_path = get_qt_plugin_path()
if os.path.exists(os.path.join(qt_plugin_path, 'platforms', 'qwindows.dll')):
    binaries.append((os.path.join(qt_plugin_path, 'platforms', 'qwindows.dll'), 'platforms'))

if os.path.exists(os.path.join(qt_plugin_path, 'imageformats', 'qjpeg.dll')):
    binaries.append((os.path.join(qt_plugin_path, 'imageformats', 'qjpeg.dll'), 'imageformats'))

if os.path.exists(os.path.join(qt_plugin_path, 'imageformats', 'qgif.dll')):
    binaries.append((os.path.join(qt_plugin_path, 'imageformats', 'qgif.dll'), 'imageformats'))

# 添加Qt翻译文件（如果存在）
qt_translations_path = get_qt_translations_path()
if os.path.exists(qt_translations_path):
    datas.append((os.path.join(qt_translations_path, 'qt_*.qm'), 'translations'))

# 添加PyQt5的Qt核心库（如果存在）
for qt_bin_path in [
    os.path.join(PYQT5_PATH, 'Qt5', 'bin'),
    os.path.join(PYQT5_PATH, 'Qt', 'bin'),
    os.path.join(os.path.dirname(PYQT5_PATH), 'Qt5', 'bin'),
]:
    if os.path.exists(qt_bin_path):
        datas.append((os.path.join(qt_bin_path, '*.dll'), ''))
        break

a = Analysis(
    ['main.py'],
    pathex=[os.getcwd()],
    binaries=binaries,
    datas=datas,
    hiddenimports=[
        'PyQt5',
        'PyQt5.QtCore',
        'PyQt5.QtGui',
        'PyQt5.QtWidgets',
        'PyQt5.QtNetwork',  # 如果用到网络功能
        'pandas',
        'openpyxl',
        'requests',
    ],
    hookspath=[],
    hooksconfig={},
    runtime_hooks=[],
    excludes=[],
    win_no_prefer_redirects=False,
    win_private_assemblies=False,
    cipher=block_cipher,
    noarchive=False,
)

pyz = PYZ(a.pure, a.zipped_data, cipher=block_cipher)

exe = EXE(
    pyz,
    a.scripts,
    a.binaries,
    a.zipfiles,
    a.datas,
    [],
    name='ZabbixTool',
    debug=False,
    bootloader_ignore_signals=False,
    strip=False,
    upx=True,
    upx_exclude=[],
    runtime_tmpdir=None,
    console=True,
    icon=os.path.join('resources', 'icon.ico') if os.path.exists(os.path.join('resources', 'icon.ico')) else None,
    disable_windowed_traceback=False,
    argv_emulation=False,
    target_arch=None,
    codesign_identity=None,
    entitlements_file=None,
)